package com.sto.stoapp.activity.aviation;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.sto.stoapp.StoApplication;
import com.sto.stoapp.activity.R;
import com.sto.stoapp.adapter.RouteListAdapter;
import com.sto.stoapp.db.StoDataBaseOperation;
import com.sto.stoapp.db.manager.RouteManager;
import com.sto.stoapp.db.manager.SqlSelectTableManager;
import com.sto.stoapp.entity.RouteEntity;
import com.sto.stoapp.http.MessageType;
import com.sto.stoapp.server.StoDownServer;
import com.sto.stoapp.thread.BaseThread;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.widget.BaseActivity;

import java.util.List;

import sto.android.app.StoPdaKeyEvent;

public class RouteListActivity extends BaseActivity {
	private ListView updateList;
	private TextView title;
	private Button backButton, updateButton, displayCount;

	private RouteListAdapter courierAdapter;
	private List<RouteEntity> courierDatas;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.sto_business_list);
		initWidget();
		initListener();
	}

	private void initWidget() {
		title = (TextView) findViewById(R.id.title);
		updateList = (ListView) findViewById(R.id.update_listview);
		updateButton = (Button) findViewById(R.id.list_update);
		backButton = (Button) findViewById(R.id.return_back);
		displayCount = (Button) findViewById(R.id.display_count);
		displayCount.setFocusable(false);
		title.setText(getString(R.string.route_list));
		BufferThread bufferThread = new BufferThread(this);
		bufferThread.start();

	}

	private void initListener() {
		updateButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				updateListData();
				Helper.showToast(RouteListActivity.this,
						getString(R.string.upgrade_data_hint));
				finish();
			}
		});

		backButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	// 更新列表数据
	private void updateListData() {
		if (checkNetWorkStatus(RouteListActivity.this)) {
			StoDataBaseOperation.getInstance(RouteListActivity.this)
					.clearTableData(SqlSelectTableManager.SQL_ROUTE_CLEAR);
			Intent downIntent = new Intent(StoDownServer.UPDATE_ACTION);
			downIntent.putExtra(StoDownServer.SERVER_DOWN_STATUS,
					new int[] { MessageType.AVIATION_ROUTE });
			sendBroadcast(downIntent);
		} else {
			Helper.showToast(RouteListActivity.this,
					getString(R.string.network_failure));
		}
	}

	class BufferThread extends BaseThread {

		private Context context;

		public BufferThread(Context context) {
			super(context);
			this.context = context;
		}

		@Override
		public void run() {
			RouteManager routeManager = RouteManager.getInstance(context);
			courierDatas = routeManager.queryAll();
			final int count = courierDatas == null ? 0 : courierDatas.size();

			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					courierAdapter = new RouteListAdapter(context,
							courierDatas);
					updateList.setAdapter(courierAdapter);
					displayCount.setText("" + count);
					if (count > 0) {
						updateList.requestFocus();
					}
				}
			});
		}
	}

	private boolean checkNetWorkStatus(Context context) {
		boolean result;
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netinfo = cm.getActiveNetworkInfo();
		if (netinfo != null && netinfo.isConnected()) {
			result = true;
		} else {
			result = false;
		}
		return result;
	}

	// 监听按键
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		keyCode= StoApplication.getStoJni().getEventFuction(keyCode);
		switch (keyCode) {
			case StoPdaKeyEvent.KEYCODE_DEL:
				finish();
				break;
		case StoPdaKeyEvent.KEYCODE_MENU:
			updateListData();
			Helper.showToast(RouteListActivity.this,
					getString(R.string.upgrade_data_hint));
			finish();
			break;
		default:
			break;
		}
		return super.onKeyUp(keyCode, event);
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if(courierDatas != null){
			courierDatas.clear();
			courierDatas = null;
		}
		super.onDestroy();
	}
}
